{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'numpy'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-1230c00b7fec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmatplotlib\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpyplot\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'numpy'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 100\n",
    "n = 5\n",
    "x = np.linspace(0, np.pi, N)\n",
    "xp = np.linspace(0, np.pi, n)\n",
    "y = np.sin(x)\n",
    "yp = np.sin(xp)\n",
    "z = np.zeros(N)\n",
    "zp = np.zeros(n)\n",
    "o = np.ones(N)\n",
    "op = np.ones(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, y, 'r--')\n",
    "plt.plot(xp, yp, 'o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lag(xp, x, i):\n",
    "    r1 = np.prod(np.array(x) - [*xp[0:i], *xp[i + 1:]])\n",
    "    r2 = np.prod(xp[i] - [*xp[0:i], *xp[i + 1:]])\n",
    "    return r1 / r2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = len(x)\n",
    "l0 = np.zeros(N)\n",
    "for i in range(0, N):\n",
    "    l0[i] = lag(xp, x[i], 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, y, 'r--')\n",
    "plt.plot(xp, yp, 'ro')\n",
    "plt.plot(x, z, 'b--')\n",
    "plt.plot(xp, zp, 'bo')\n",
    "plt.plot(x, o, 'k--')\n",
    "plt.plot(xp, op, 'ko')\n",
    "plt.plot(x, l0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = np.zeros(N)\n",
    "for i in range(0, N):\n",
    "    l1[i] = lag(xp, x[i], 1)\n",
    "l2 = np.zeros(N)\n",
    "for i in range(0, N):\n",
    "    l2[i] = lag(xp, x[i], 2)\n",
    "l3 = np.zeros(N)\n",
    "for i in range(0, N):\n",
    "    l3[i] = lag(xp, x[i], 3)\n",
    "l4 = np.zeros(N)\n",
    "for i in range(0, N):\n",
    "    l4[i] = lag(xp, x[i], 4)   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, y, 'r--')\n",
    "plt.plot(xp, yp, 'ro')\n",
    "plt.plot(x, z, 'b--')\n",
    "plt.plot(xp, zp, 'bo')\n",
    "plt.plot(x, o, 'k--')\n",
    "plt.plot(xp, op, 'ko')\n",
    "plt.plot(x, l0)\n",
    "plt.plot(x, l1)\n",
    "plt.plot(x, l2)\n",
    "plt.plot(x, l3)\n",
    "plt.plot(x, l4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Runge(x):\n",
    "    return 1.0 / (1.0 + x**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 10\n",
    "xp = np.linspace(-5, 5, n)\n",
    "yp = Runge(xp)\n",
    "l = np.zeros((n, N))\n",
    "x = np.linspace(-5, 5, N)\n",
    "for i in range(0, n):\n",
    "    for j in range(0, N):\n",
    "        l[i][j] = lag(xp, x[j], i)\n",
    "y = np.zeros(N)\n",
    "for j in range(0, N):\n",
    "    for i in range(0, n):\n",
    "        y[j] = y[j] +  l[i][j] * yp[i] \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, Runge(x), '--')\n",
    "plt.plot(x, y, 'b')\n",
    "plt.plot(xp, yp, 'ro')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 10\n",
    "xp = np.linspace(-1, 1, n)\n",
    "l = np.zeros((n, N))\n",
    "x = np.linspace(-1, 1, N)\n",
    "for i in range(0, n):\n",
    "    for j in range(0, N):\n",
    "        l[i][j] = lag(xp, x[j], i)\n",
    "zp = np.zeros(n)\n",
    "op = np.ones(n)\n",
    "for i in range(n):\n",
    "    plt.plot(x, l[i])\n",
    "plt.plot(xp, zp, 'bo')\n",
    "plt.plot(xp, op, 'bo')\n",
    "T = np.cos(n * np.arccos(x))\n",
    "plt.plot(x, T, 'k--')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
